<?php

/**
 * DO NOT USE!
 * CSV Renderer for Report class
 * @ignore
 * @author DEC Web Solutions.
 */
class Pdfrender {

	/**
	 * Default font size used in this Report
	 *
	 * @var integer
	 */
	private $fontSize = 10;

	/**
	 * Renders the Table header
	 *
	 * @param PDF 		$pdf
	 * @param array 	$header
	 * @param array 	$width
	 * @param integer	$height
	 */
	private function renderTableHeader($pdf, $header, $width, $height) {
		// Colors, line width and bold font
		$pdf->SetFillColor(239, 50, 0);
		$pdf->SetTextColor(255);
		$pdf->SetLineWidth(.2);
		$pdf->SetFont('', 'B');
		foreach ($header as $key => $value) {
			$pdf->Cell($width[$key], $height, $value, 1, 0, 'C', true);
		}
		$pdf->Ln();
	}

	/**
	 * Renders the Data Table
	 *
	 * @param PDF 		$pdf
	 * @param array 	$header
	 * @param array 	$data
	 * @param integer 	$cellHeight
	 * @param array 	$align
	 */
	private function renderTable($pdf, $header, $data, $cellHeight, $align) {
		$width = $this->getWidth($pdf, $header, $data);
		$fill = false;
		// Printing header
		$this->renderTableHeader($pdf, $header, $width, $cellHeight);
		// Color and font restoration
		$pdf->SetFillColor(224, 235, 255);
		$pdf->SetTextColor(0);
		$pdf->SetFont('');
		foreach ($data as $key => $row) {
			$maxHeight = 7;
			// Getting max height before render line cells
			foreach ($row as $key => $value) {
				$lines = $pdf->WordWrap($value, $width[$key]);
				if (!$lines)
					$lines = 1;
				$height = $lines * $cellHeight;
				if ($height > $maxHeight)
					$maxHeight = $height;
			}
			// Rendering line cells
			foreach ($row as $key => $value) {
				$pdf->Cell($width[$key], $maxHeight, $value, 1, 0, $align[$key], $fill);
			}
			$pdf->Ln();
			$fill = !$fill;
		}
	}

	/**
	 * Calculates the width for each column
	 *
	 * @param PDF		$pdf
	 * @param array		$header
	 * @param array		$data
	 *
	 * @return array
	 */
	private function getWidth($pdf, $header, $data) {
		$numberOfColumns = sizeof($header);
		// Initial width is the header width
		foreach ($header as $key => $text) {
			$width[$key] = (integer) ceil($pdf->GetStringWidth($text));
		}
		// Fetching max width for each column
		foreach ($data as $row) {
			foreach ($row as $key => $cell) {
				$cellWidth = $pdf->GetStringWidth($cell);
				if ($cellWidth > $width[$key])
					$width[$key] = (integer) ceil($cellWidth);
			}
		}
		// Checking total width
		$total = 0;
		$greater = 0;
		$greaterValue = 0;
		$i = 0;
		foreach ($width as $key => $value) {
			if ($value > $greaterValue) {
				$greater = $key;
				$greaterValue = $value;
			}
			$total+= $value;
		}
		// Reducing (when needed)
		if ($total > $pdf->getViewportWidth()) {
			$width[$greater] = $pdf->getViewportWidth() - ($total - $width[$greater]);
			if ($width[$greater] < 0)
				$width[$greater] = $pdf->getViewportWidth() / $numberOfColumns;
			// Expanding (when needed)
		} else {
			$difference = $pdf->getViewportWidth() - $total;
			foreach ($width as $key => $value) {
				$width[$key] = $width[$key] + $difference / $numberOfColumns;
			}
		}
		return $width;
	}

	/**
	 * Renders the document header
	 *
	 * @param PDF		$pdf
	 * @param string	$title
	 * @param integer	$lineHeight
	 * @param Date 		$date
	 * @param integer	$total
	 */
	private function renderReportHeader($pdf, $title, $system_name, $lineHeight, $total) {
		$pdf->SetFont('Helvetica');
		$pdf->AddPage();
		$pdf->SetFontSize($this->fontSize + 6);
		$pdf->Write($lineHeight, $system_name);
		$pdf->Ln();
		$pdf->Ln();
		if ($title) {
			$pdf->SetFont("", "B", $this->fontSize);
			$pdf->Write($lineHeight, __("Report") . ": ");
			$pdf->SetFont("");
			$pdf->Write($lineHeight, $title);
			$pdf->Ln();
		}
		$pdf->SetFont("", "B", $this->fontSize);
		$pdf->Write($lineHeight, __("Requested By") . ": ");
		$pdf->SetFont("");
		if (Model_User::logged_in()) {
			$user = Model_User::get_user();
			$user = $user->get_name();
		} else {
			$user = __("Visitor");
		}
		$pdf->Write($lineHeight, $user);
		$pdf->Ln();
		$pdf->SetFont("", "B", $this->fontSize);
		$pdf->Write($lineHeight, __("Generated At") . ": ");
		$pdf->SetFont("");
		$pdf->Write($lineHeight, date("Y-m-d H:i:s"));
		$pdf->Ln();
		$pdf->SetFont("", "B", $this->fontSize);
		$pdf->Write($lineHeight, __("Report URL") . ": ");
		$pdf->SetFont("");
		$pdf->SetTextColor(0, 0, 255);
		if (!empty($_SERVER['HTTPS'])) {
			$pdf->Write($lineHeight, "https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
		} else {
			$pdf->Write($lineHeight, "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
		}
		$pdf->Ln();
		$pdf->Ln();
		$pdf->SetTextColor(0, 0, 0);
		$pdf->Write($lineHeight, __("Report Total") . " " . $total);
		$pdf->Ln();
		$pdf->Ln();
		$pdf->SetFontSize($this->fontSize);
	}

	/**
	 * Renders the PDF Report
	 */
	public function render($header, $data, $align, $system_name="", $title="") {
		$filename = "data_" . date("Y-m-d_H:i");
		$pdf = new PDF("L");
		$lineHeight = $this->fontSize / 3.5 + 2;
		$this->renderReportHeader($pdf, $title, $system_name, $lineHeight, sizeof($data));
		$this->renderTable($pdf, $header, $data, $lineHeight, $align);
		$pdf->Output($filename . ".pdf", "I");
		die;
	}

} // End Pdf Render